<?php

/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2018/1/23
 * Time: 21:54
 */

namespace app\admin\controller;

use app\common\business\LogBusiness;
use app\common\consts\AdminConst;
use app\common\helper\ResponseHelper;
use think\Controller;
use think\Db;
use think\facade\Request;

abstract class Base extends Controller
{
    protected $admin;

    protected $loginId = 0;

    protected $menu;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }


    public function getMenu()
    {
        if($this->admin->type == AdminConst::TYPE_CHANNEL){
            $list = [[
                'id' => 3,
                'pid' => 5,
                'name' => 'admin/channel.get.data.list',
                'title' => '数据统计',
                'css' => ''
            ]];
        }else{
            $list = [
                [
                    'id' => 2,
                    'pid' => 1,
                    'name' => 'admin/get.product.list',
                    'title' => '产品管理',
                    'css' => ''
                ],
                [
                    'id' => 3,
                    'pid' => 1,
                    'name' => 'admin/get.channel.list',
                    'title' => '渠道列表',
                    'css' => ''
                ],
                [
                    'id' => 3,
                    'pid' => 5,
                    'name' => 'admin/get.product.data.list',
                    'title' => '数据统计',
                    'css' => ''
                ],
                [
                    'id' => 4,
                    'pid' => 1,
                    'name' => 'admin/get.user.list',
                    'title' => '用户列表',
                    'css' => ''
                ]
            ];
        }
        return [
            [
                'id' => 1,
                'pid' => 0,
                'name' => 'Data',
                'title' => '贷超管理',
                'css' => '',
                'list' => $list
            ]
        ];
    }

    /**
     * 获取管理员分组列表
     * @return array
     */
    protected function getAdminGroupList()
    {
        return Db::name('auth_group')->where('status=1')->column('id,title');
    }

    /**
     * 写日志
     * @param array $log
     * @return bool|mixed
     */
    protected function writeLog($log = [])
    {
        $log = $log ? $log : Request::instance()->request();
        $data = [
            'admin_id' => $this->admin['id'],
            'controller' => Request::instance()->controller(),
            'action' => Request::instance()->action(),
            'log' => ResponseHelper::jsonEncode($log),
        ];
        return LogBusiness::getInstance()->add($data);
    }

    /**
     * 操作
     * @param $status
     */
    protected static function doLast($status)
    {
        if ($status === false) {
            ResponseHelper::_failure(ResponseHelper::COMMON_ERROR_MESSAGE);
        }
        ResponseHelper::_success(ResponseHelper::COMMON_SUCCESS_MESSAGE);
    }
}